<?php
//require APPLICATION_PATH . '/modules/default/models/Sms_Reponse.php';
class Sms {

    /**
     * Sevice Sms
     *
     * @param int $MOId
     * @param string $Telco
     * @param string $ServiceNum
     * @param string $Phone
     * @param string $Syntax
     * @param string $Message
     * @return array
     */
    public function SendSms($MOId, $Telco, $ServiceNum, $Phone, $Syntax, $Message) {
        $successed = array('SendSMSResult' => 1,  'MessageReturn' => 'Du doan thanh cong. Cam on ban da tham gia chuong trinh cua VnE The Thao. Chuc ban may man.');
        $failed    = array('SendSMSResult' => -1, 'MessageReturn' => 'Du doan khong thanh cong. De duoc ho tro, vui long lien he so dien thoai (08) 7300 9999 (so noi bo: 8541) hoac email trogiup@vnexpress.net');
        
        try {
            //Validate MO Data
            $valid = $this->moValidate($MOId, $Telco, $ServiceNum, $Phone, $Syntax, $Message);
            if (!$valid) {     
                $this->moTracking ($MOId, $Telco, $ServiceNum, $Phone, $Syntax, $Message, -1, 'Du doan khong thanh cong. De duoc ho tro, vui long lien he so dien thoai (08) 7300 9999 (so noi bo: 8541) hoac email trogiup@vnexpress.net');
                return $failed;
            }
            
            //Valid Format Message
            $arrMessage = explode(' ', trim($Message));
            if (count($arrMessage)!=3) {                   
                $this->moTracking ($MOId, $Telco, $ServiceNum, $Phone, $Syntax, $Message, -1, 'Du doan khong thanh cong. De duoc ho tro, vui long lien he so dien thoai (08) 7300 9999 (so noi bo: 8541) hoac email trogiup@vnexpress.net');
                return $failed;
            }
            
            list($msgSyntax, $msgMatchCode, $msgCodePredict) = $arrMessage;

            //Get match
            $instanceMatchExtend = MatchExtend::getInstance();
            $arrMatchExtend      = $instanceMatchExtend->getDetailMatchExtendByCode($msgMatchCode);
            if (empty($arrMatchExtend[0])) {                
                $this->moTracking ($MOId, $Telco, $ServiceNum, $Phone, $Syntax, $Message, -1, 'Du doan khong thanh cong. De duoc ho tro, vui long lien he so dien thoai (08) 7300 9999 (so noi bo: 8541) hoac email trogiup@vnexpress.net', NULL, $msgCodePredict);
                return $failed;
            }
            
            //Update send_sms 
            $success = $instanceMatchExtend->updateMatchPredict($arrMatchExtend[0]['MatchID'], $Phone, $msgCodePredict);
            if (!$success) {                
                $this->moTracking ($MOId, $Telco, $ServiceNum, $Phone, $Syntax, $Message, -1, 'Du doan khong thanh cong. De duoc ho tro, vui long lien he so dien thoai (08) 7300 9999 (so noi bo: 8541) hoac email trogiup@vnexpress.net', NULL, $msgCodePredict);
                return $failed;
            }
        } catch (Exception $e) {            
            $this->moTracking ($MOId, $Telco, $ServiceNum, $Phone, $Syntax, $Message, -1, 'Du doan khong thanh cong. De duoc ho tro, vui long lien he so dien thoai (08) 7300 9999 (so noi bo: 8541) hoac email trogiup@vnexpress.net');
            return $failed;
        }        
        $this->moTracking ($MOId, $Telco, $ServiceNum, $Phone, $Syntax, $Message, 1, 'Du doan thanh cong. Cam on ban da tham gia chuong trinh cua VnE The Thao. Chuc ban may man.', NULL, $msgCodePredict);
        return $successed;
    }
    
    /**
     * 
     * validate MO data 
     * @param string   $requestID
     * @param string   $userID
     * @param string   $serviceID
     * @param string   $commandCode
     * @param string   $message
     * @param string   $operator
     * @param dateTime $requestTime
     * @return bool
     */
    private function moValidate($MOId, $Telco, $ServiceNum, $Phone, $Syntax, $Message)
    {       
        $ServiceAvalible  = array('8500');
        $Telcos = array('vina', 'mobi', 'viettel', 'evn', 'beeline', 'htc', 'sfone');
        
        if (!$MOId || (int)$MOId < 0)
        {
            return false;
        }
        if(!$Telcos || strlen($Telcos) > 10 || !in_array($Telco, $Telcos))
        {
           return false;
        }        
        if(!$ServiceNum || strlen($ServiceNum) > 10 || !in_array($ServiceNum, $ServiceAvalible))
        {
           return false;
        }
        if(!$Phone || strlen($Phone) > 20)
        {
           return false;
        }
        
        if(!$Syntax || strlen($Syntax) > 50 || strtolower($Syntax) != 'vne')
        {
           return false;
        }
        if(!$Message || strlen($Message) > 160)
        {
           return false;
        }        
        return true;      
    }
    
    private function moTracking ($MOId, $Telco, $ServiceNum, $Phone, $Syntax, $Message, $ReturnCode = NULL, $ReturnMSG = NULL, $NumberPeople = NULL, $CodePredict = NULL) {
        $instanceMatchExtend = MatchExtend::getInstance();
         
        //Insert tracking
        $params = array('moid'           => $MOId,
                        'telco'          => $Telco,
                        'service_num'    => $ServiceNum,
                        'phone'          => $Phone,
                        'syntax'         => $Syntax,
                        'message'        => $Message,
                        'return'         => $ReturnCode,
                        'message_return' => $ReturnMSG,
                        'num_people'     => $NumberPeople,
                        'code_predict'   => $CodePredict
        );
        
        return $instanceMatchExtend->insertSmsPredict($params);
    }
}